#include "Dwarf.h"

Dwarf::Dwarf(){
}

Dwarf::Dwarf(DwarfType x){
	dwarfType = x;
	dwarfStatus = WAKE_UP;
	switch(x){
	case WATERING_DWARF_CODE :
		curPos.x = DEFAULT_POS_DWARF1_X;
		curPos.y = DEFAULT_POS_DWARF1_Y;
		curDir = UP;
		break;
	case HARVESTING_DWARF_CODE :
		curPos.x = DEFAULT_POS_DWARF2_X;
		curPos.y = DEFAULT_POS_DWARF2_Y;
		curDir = RIGHT;
		break;
	case SLASHING_DWARF_CODE :
		curPos.x = DEFAULT_POS_DWARF3_X;
		curPos.y = DEFAULT_POS_DWARF3_Y;
		curDir = DOWN;
		break;
	}
}

Dwarf::~Dwarf(){
}

void Dwarf::initialize(SaveInfo saveInfo){
    dwarfStatus = saveInfo.ADI[dwarfType-1].status;
    curPos = saveInfo.ADI[dwarfType-1].curPos;
    curDir = saveInfo.ADI[dwarfType-1].curDir;
}

//get & set
//change dwarf's Status, status WAKE_UP / SLEEP
void Dwarf::setStatus(DwarfStatus newStatus){
	dwarfStatus = newStatus;
}

//get dwarf status (WAKE_UP = 1 /SLEEP = 0)
DwarfStatus Dwarf::getStatus() {
	return dwarfStatus;
}

//get dwarf type
DwarfType Dwarf::getType(){
	return dwarfType;
}

//method
//Act based on dwarf type.

// dari current positionnya, dwarf melihat sekelilingnya; jika ada yang dikerjakan, throw. (act selesai)
int Dwarf::act(Farm& farm,DwarfType jenisDwarf){
	int i;
	int total = 0;
	FruitType tipeBuah;
	try{
		switch(jenisDwarf){
		case WATERING_DWARF_CODE :{
			for (i=0; i<4; i++){	//cek ke empat sisi
				if (!isFaceOutside())
					if (water(farm)) 				//jika ada yang bisa di water, throw bool
						throw true;
					else
						nextLook(curDir);
				else
					nextLook(curDir);		// jika tidak ada, ubah direction
			}
			for (i=0; i<4; i++){			//jika tidak ada yang bisa di water, cari jalan yang dapat dilalui
				if (!isFaceOutside())
					if(move(farm))
						throw true;
					else
						nextLook(curDir);
				else
					nextLook(curDir);
			}
			break;}
		case HARVESTING_DWARF_CODE :{
			for (i=0; i<4; i++){
				if (!isFaceOutside())
					if(farm.isHarvestable(getFrontPos())){
						tipeBuah= harvest(farm);
						total = getPrice(tipeBuah);
						throw true;
					}
					else
						nextLook(curDir);
				else
					nextLook(curDir);
			}
			for (i=0; i<4; i++){
				if (!isFaceOutside())
					if(move(farm))
						throw true;
					else
						nextLook(curDir);
				else
					nextLook(curDir);
			}
			break;}
		case SLASHING_DWARF_CODE :{
			for (i=0; i<4; i++){
				if(!isFaceOutside())
					if (slash(farm))
						throw true;
					else
						nextLook(curDir);
				else
					nextLook(curDir);
			}
			for (i=0; i<4; i++){
				if (!isFaceOutside()){
					if(move(farm))
						throw true;
					else
						nextLook(curDir);
				}
				else
					nextLook(curDir);
			}
			break;}
		}
	}
	catch (bool doSomething)		//hanya menangkap lemparan dan memberhentikan method
	{}
	return total;
}


//method harvest, if harvestable, increase player balance and setHarvested

//pengecekan isHarvestable nya dilakukan di method act
FruitType Dwarf::harvest(Farm& farm){
		FruitType fruitType = farm.getFruitType(getFrontPos());
		farm.setHarvested(getFrontPos());
		return fruitType;
}

// method untuk merubah direction dwarf ketika mencari apa yang dapat dikerjakannya.
void Dwarf::nextLook(Direction nowDirection){

	switch(nowDirection){
		case UP :
			changeDir(RIGHT);
		break;
		case RIGHT :
			changeDir(DOWN);
		break;
		case DOWN :
			changeDir(LEFT);
		break;
		case LEFT :
			changeDir(UP);
		break;
	}
}

void Dwarf::getSaveInfo(SaveInfo& saveInfo){
    saveInfo.ADI[dwarfType-1].curPos = curPos;
    saveInfo.ADI[dwarfType-1].curDir = curDir;
    saveInfo.ADI[dwarfType-1].status = dwarfStatus;
    saveInfo.ADI[dwarfType-1].type = dwarfType;
}
